package edu.biwu.mapper;

import edu.biwu.po.Product;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface ProductMapper {

    /**SelectKey 在进行插入动作完成之前或者后面查询主键
     * keyProperty:主键对应属性
     * keyColumn:主键列
     * resultType:主键的java类型
     * statement:执行的语句
     * before:true,在插入语句之前查询主键,false在插入语句之后查询主键
     * @param product
     */
    @SelectKey(keyProperty = "id",keyColumn = "id",resultType = int.class,statement = "SELECT LAST_INSERT_ID()",before = false)
    @Insert("INSERT INTO mybatis02.product(name,price,detail,pic,createtime) " +
            "VALUES (#{name} ,#{price} ,#{detail} ,#{pic} ,#{createtime} )")
    void savaProduct(Product product);

    @Update("UPDATE mybatis02.product SET name = #{name},price = #{price},detail = #{detail},pic = #{pic},createtime = #{createtime} WHERE id= #{id}")
    void updateProduct(Product product);

    @Delete("DELETE FROM mybatis02.product WHERE id = #{id}")
    void deleteProduct(int id);

    @Select("SELECT * FROM mybatis02.product")
    List<Product> selectdAll();

    /**
     * //@Results注解相当于映射文件中的<resultMap></resultMap>
     * //@Result注解相当于映射文件中的<result></result>
     * //id属性相当于<id></id>
     * id = true 相当于将result标签转化为id标签
     * @param id
     * @return
     */
    @Results({
            @Result(column = "pid",property = "id",id = true),
            @Result(column = "pname",property = "name")
    })
    @Select("SELECT id AS pid,name AS pname,price,detail,createtime FROM mybatis02.product WHERE id = #{id}")
    Product findById(int id);


}
